/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * CustomersList.java
 *
 * Created on 10/06/2011, 01:55:01
 */

package com.classicmodels.view;

import com.classicmodels.controller.CustomerController;
import com.classicmodels.model.Customer;
import com.classicmodels.model.CustomerDAO;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumnModel;


/**
 *
 * @author mauricio
 */
public class CustomersList extends javax.swing.JFrame {
    private DefaultTableModel modelo = new DefaultTableModel();
    private Customer[] c;
    /** Creates new form CustomersList */
    


    /** Creates new form CustomersList */
    

    public CustomersList(Customer[] customers) {
        initComponents();
        c = customers;
        SetModelo();
        tabla = new javax.swing.JTable(modelo);
        jScrollPane1.setViewportView(tabla);
        SetAnchoColumna();
        
    }

   

  

    

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        deleteButton = new javax.swing.JButton();
        newButton = new javax.swing.JButton();
        editCustomer = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabla = new javax.swing.JTable();

        setTitle("Customers List");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Ubuntu", 1, 18));
        jLabel1.setText("Classic Models Inc - Customers");

        deleteButton.setText("Delete");
        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        newButton.setText("New Customer");
        newButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newButtonActionPerformed(evt);
            }
        });

        editCustomer.setText("Edit Customer");
        editCustomer.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editCustomerActionPerformed(evt);
            }
        });

        tabla.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tabla.setAutoResizeMode(javax.swing.JTable.AUTO_RESIZE_ALL_COLUMNS);
        jScrollPane1.setViewportView(tabla);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel1))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 1266, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(deleteButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 494, Short.MAX_VALUE)
                        .addComponent(editCustomer)
                        .addGap(476, 476, 476)
                        .addComponent(newButton)
                        .addGap(13, 13, 13)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 239, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(37, 37, 37)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(newButton)
                    .addComponent(deleteButton)
                    .addComponent(editCustomer))
                .addContainerGap(47, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void editCustomerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editCustomerActionPerformed
        int reg = tabla.getSelectedRow();
        int col = tabla.getSelectedColumn();
        String[] customer = new String[13];
        int i = 0;
        for (col = 0, i = 0; col < 13; col++, i++) {
             customer[i] = String.valueOf(this.tabla.getValueAt(reg, col));
        }
        this.dispose();
        CustomerController.abrirCustomerFormEdit(customer);
        this.dispose();
        
        
        
    }//GEN-LAST:event_editCustomerActionPerformed

    private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
        try {
            this.dispose();
            CustomerController.abrirCustomerForm();
            this.dispose();
        } catch (Exception ex) {
            Logger.getLogger(CustomersList.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
    }//GEN-LAST:event_newButtonActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        // TODO add your handling code here:
    }//GEN-LAST:event_formWindowClosed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        int reg = tabla.getSelectedRow();
        int col = tabla.getSelectedColumn();
        col = 0;
        String aux = String.valueOf(this.tabla.getValueAt(reg, col));
        int customerNumber = Integer.parseInt(aux);
        try {
            this.dispose();
            CustomerDAO.delete(customerNumber);
            this.dispose();
        } catch (Exception ex) {
            Logger.getLogger(CustomersList.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        
        
        
        
    }//GEN-LAST:event_deleteButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                //new CustomersList().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton deleteButton;
    private javax.swing.JButton editCustomer;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton newButton;
    private javax.swing.JTable tabla;
    // End of variables declaration//GEN-END:variables
public void SetModelo(){
    String[] cus = Customer.getVectorAtributesNames();
    //Cargo el nombre de las columnas    
    for(String nombreAtributo : cus){
            modelo.addColumn(nombreAtributo);
            
    }
    
    //cargos los datoa 1 x 1
    for (Customer cust : c){
        modelo.addRow(cust.getVectorCustomerData());
    }
}

public void SetAnchoColumna(){
    TableColumnModel colmod = tabla.getColumnModel();
        colmod.getColumn(0).setPreferredWidth(40);
        colmod.getColumn(1).setPreferredWidth(300);
        colmod.getColumn(2).setPreferredWidth(150);
        colmod.getColumn(3).setPreferredWidth(105);
        colmod.getColumn(4).setPreferredWidth(225);
        colmod.getColumn(5).setPreferredWidth(250);
        colmod.getColumn(6).setPreferredWidth(190);
        colmod.getColumn(7).setPreferredWidth(175);
        colmod.getColumn(8).setPreferredWidth(110);
        colmod.getColumn(9).setPreferredWidth(100);
        colmod.getColumn(10).setPreferredWidth(130);
        colmod.getColumn(11).setPreferredWidth(50);
        colmod.getColumn(12).setPreferredWidth(70);
}
}



